package com.cl.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cl.annotation.IgnoreAuth;
import com.cl.annotation.SysLog;
import com.cl.entity.TiaobanshenqingEntity;
import com.cl.entity.YishengpaibanEntity;
import com.cl.entity.view.TiaobanshenqingView;
import com.cl.service.TiaobanshenqingService;
import com.cl.service.YishengpaibanService;
import com.cl.utils.MPUtil;
import com.cl.utils.PageUtils;
import com.cl.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 医生调班申请
 * 后端接口
 * @author 
 * @email 
 * @date 2024-05-09 22:03:58
 */
@RestController
@RequestMapping("/tiaobanshenqing")
public class TiaobanshenqingController {
    @Autowired
    private TiaobanshenqingService tiaobanshenqingService;

    @Autowired
    private YishengpaibanService yishengpaibanService;



    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, TiaobanshenqingEntity tiaobanshenqingEntity,
                  HttpServletRequest request){
		String tableName = request.getSession().getAttribute("tableName").toString();
        if(tableName.equals("yisheng")) {
            tiaobanshenqingEntity.setYishengzhanghao((String)request.getSession().getAttribute("username"));
        }
        if(tableName.equals("zhensuoguanliyuan")) {
            tiaobanshenqingEntity.setGuanlizhanghao((String)request.getSession().getAttribute("username"));
        }
        EntityWrapper<TiaobanshenqingEntity> ew = new EntityWrapper<>();

		PageUtils page = tiaobanshenqingService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, tiaobanshenqingEntity), params), params));

        return R.ok().put("data", page);
    }
    
    /**
     * 前端列表
     */
	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, TiaobanshenqingEntity tiaobanshenqing,
                  HttpServletRequest request){
        EntityWrapper<TiaobanshenqingEntity> ew = new EntityWrapper<TiaobanshenqingEntity>();

		PageUtils page = tiaobanshenqingService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, tiaobanshenqing), params), params));
        return R.ok().put("data", page);
    }

	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( TiaobanshenqingEntity tiaobanshenqing){
       	EntityWrapper<TiaobanshenqingEntity> ew = new EntityWrapper<TiaobanshenqingEntity>();
      	ew.allEq(MPUtil.allEQMapPre( tiaobanshenqing, "tiaobanshenqing")); 
        return R.ok().put("data", tiaobanshenqingService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(TiaobanshenqingEntity tiaobanshenqing){
        EntityWrapper<TiaobanshenqingEntity> ew = new EntityWrapper<TiaobanshenqingEntity>();
 		ew.allEq(MPUtil.allEQMapPre( tiaobanshenqing, "tiaobanshenqing")); 
		TiaobanshenqingView tiaobanshenqingView =  tiaobanshenqingService.selectView(ew);
		return R.ok("查询医生调班申请成功").put("data", tiaobanshenqingView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        TiaobanshenqingEntity tiaobanshenqing = tiaobanshenqingService.selectById(id);
		tiaobanshenqing = tiaobanshenqingService.selectView(new EntityWrapper<TiaobanshenqingEntity>().eq("id", id));
        return R.ok().put("data", tiaobanshenqing);
    }

    /**
     * 前端详情
     */
	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        TiaobanshenqingEntity tiaobanshenqing = tiaobanshenqingService.selectById(id);
		tiaobanshenqing = tiaobanshenqingService.selectView(new EntityWrapper<TiaobanshenqingEntity>().eq("id", id));
        return R.ok().put("data", tiaobanshenqing);
    }
    



    /**
     * 后端保存
     */
    @RequestMapping("/save")
    @SysLog("新增医生调班申请")
    public R save(@RequestBody TiaobanshenqingEntity tiaobanshenqing, HttpServletRequest request){
    	tiaobanshenqing.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(tiaobanshenqing);
        tiaobanshenqingService.insert(tiaobanshenqing);
        return R.ok();
    }
    
    /**
     * 前端保存
     */
    @SysLog("新增医生调班申请")
    @RequestMapping("/add")
    public R add(@RequestBody TiaobanshenqingEntity tiaobanshenqing, HttpServletRequest request){
    	tiaobanshenqing.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(tiaobanshenqing);
        tiaobanshenqingService.insert(tiaobanshenqing);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改医生调班申请")
    public R update(@RequestBody TiaobanshenqingEntity tiaobanshenqing, HttpServletRequest request){
        //ValidatorUtils.validateEntity(tiaobanshenqing);
        tiaobanshenqingService.updateById(tiaobanshenqing);//全部更新
        return R.ok();
    }



    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除医生调班申请")
    public R delete(@RequestBody Long[] ids){
        tiaobanshenqingService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/shenpi/{id}/{shenizhuangtai}/{shenpiyijian}")
    @SysLog("删除医生调班申请")
    public R shenpi(@PathVariable Long id, @PathVariable  String shenpizhuangtai, @PathVariable  String shenpiyijian) {
        // 更新调班申请状态
        TiaobanshenqingEntity tiaobanshenqing = tiaobanshenqingService.selectById(id);
        tiaobanshenqing.setShenpiyijian(shenpiyijian);
        tiaobanshenqing.setShenpizhuangtai(shenpizhuangtai);
        tiaobanshenqingService.updateById(tiaobanshenqing);
        // 根据不同的状态，处理排班记录
        YishengpaibanEntity yishengpaiban = yishengpaibanService.selectById(tiaobanshenqing.getYishengpaibanid());
        if ("通过".equals(shenpizhuangtai)) {
            YishengpaibanEntity newYishengpaiban = new YishengpaibanEntity();
            BeanUtils.copyProperties(tiaobanshenqing, newYishengpaiban);
            newYishengpaiban.setId(yishengpaiban.getId());
            yishengpaibanService.updateById(newYishengpaiban);
        } else if ("驳回".equals(shenpizhuangtai)) {
           // 啥也不用干
        }
        return R.ok();
    }







}
